import bagel.Drawing;
import bagel.Image;
import bagel.util.Colour;
import bagel.util.Point;
import bagel.util.Rectangle;

public class Enemy {
    private Point position;
    private Image image;
    private double width,height;
    private CollisionManager collisionManager;
    private boolean onPlatform = false;
    private double verticalSpeed = 0;

    public Enemy(Point position, String imagePath, CollisionManager collisionManager) {
        this.position = position;
        this.image = new Image(imagePath);
        this.width = image.getWidth();
        this.height = image.getHeight();
        this.collisionManager = collisionManager;
    }

    public void update(){
        if(onPlatform){
            return;
        }
        verticalSpeed += Common.GRAVITY;
        position = new Point(position.x, position.y + verticalSpeed);

        Point tmp = collisionManager.OnPlatform(position, width, height);
        if(null!=tmp){
            position = tmp;
            onPlatform=true;
        }
        
    }

    public Point getPosition() {
        return position;
    }

    public Image getImage() {
        return image;
    }

    public Rectangle getBoundingBox(){
        return new Rectangle(position.x-width/2,position.y-height/2,width,height);
    }

    public void render() {
        image.draw(position.x, position.y);
        /*
         // 计算碰撞箱的四个角的坐标
        double left = position.x - width / 2;
        double right = position.x + width / 2;
        double top = position.y - height / 2;
        double bottom = position.y + height / 2;
        Point lt = new Point(left,top);
        Point rt = new Point(right,top);
        Point lb = new Point(left,bottom);
        Point rb = new Point(right,bottom);

        // 绘制矩形的四条边
        Drawing.drawLine(lt,rt,1, Colour.RED); // 上边
        Drawing.drawLine(rt,rb, 1,Colour.RED); // 右边
        Drawing.drawLine(rb,lb, 1,Colour.RED); // 下边
        Drawing.drawLine(lb,lt, 1,Colour.RED); // 左边
         */
    }
    public double getWidth(){
        return width;
    }
    public double getHeight(){
        return height;
    }
}